﻿Imports System.Linq
'Imports System.Runtime.Serialization

Namespace Configuration.Templating.Context

    <Serializable()>
    Public Class DNNTemplatingContext
        Implements ITemplatingContext

        Public Class DNNTemplatingContextParent
            Inherits DNNTemplatingContext

            Protected Friend Sub New(ChildTemplateType As String)
                MyBase.New(ChildTemplateType)
            End Sub

            Protected Overrides Sub SetupLevels()
                ' le contexte ne fait rien, il est géré par son créateur
            End Sub

        End Class

        Public Property Separator As String = "."

        Public Property TemplateType As String
        Public Property CurrentCulture As String
        Public Property Content As String

        Public Property ContentDisplayName As String

        Protected Property CurrentContentLevel As Integer = 0
        Protected Property CurrentLanguageLevel As Integer = 0

        Public Sub New(Template As String)
            TemplateType = Template
        End Sub

        Public Function GetParentContext() As ITemplatingContext Implements ITemplatingContext.GetParentContext

            Dim NewContentLevel As Integer = CurrentContentLevel
            Dim NewLanguageLevel As Integer = CurrentLanguageLevel

            NewLanguageLevel -= 1

            If NewLanguageLevel < 0 Then
                If NewContentLevel > 0 Then
                    NewContentLevel -= 1
                    NewLanguageLevel = GetLanguageLevel()
                End If
            End If

            If NewLanguageLevel < 0 Then
                Return Nothing
            End If

            Return New DNNTemplatingContextParent(TemplateType) With {.Content = Content _
                                                                        , .ContentDisplayName = ContentDisplayName _
                                                                        , .CurrentCulture = CurrentCulture _
                                                                        , .CurrentContentLevel = NewContentLevel _
                                                                        , .CurrentLanguageLevel = NewLanguageLevel}

        End Function

        Public ReadOnly Property Key As String Implements ITemplatingContext.Key
            Get
                SetupLevels()

                Dim CurrentKeys As New List(Of String)
                CurrentKeys.Add(TemplateType)

                Select Case CurrentContentLevel
                    Case 1
                        CurrentKeys.Add(Content)
                    Case 0
                        ' on n'ajoute rien
                End Select

                Select Case CurrentLanguageLevel
                    Case 1
                        CurrentKeys.Add(CurrentCulture)
                    Case 0
                        ' on n'ajoute rien
                End Select

                Return String.Join(Separator, CurrentKeys.ToArray())
            End Get
        End Property

        Protected Overridable Sub SetupLevels()
            CurrentLanguageLevel = GetLanguageLevel()
            CurrentContentLevel = GetContentLevel()
        End Sub

        Protected Function GetLanguageLevel() As Integer
            Dim value As Integer = 0

            If Not String.IsNullOrEmpty(CurrentCulture) Then
                value += 1
            End If

            Return value
        End Function

        Protected Function GetContentLevel() As Integer
            Dim value As Integer = 0
            If Not String.IsNullOrEmpty(Content) Then
                value += 1
            End If

          
            Return value
        End Function
    End Class
End Namespace